<?php

namespace StudyBuddy\Modules\Observers;

/**
 * Posting flood check filter
 * This filter is implemented as
 * the Observer
 *
 */
class FloodCheck extends \StudyBuddy\Observer {

    /**
     * @todo later we should check the time
     * offset based on user reputation score
     *
     */
    protected $minutesToWait = 1;

    public function main() {

        if (STUDYBUDDY_DEBUG) {
            d('flood check not performed in debug mode');

            return;
        }

        $this->minutesToWait = (int) $this->oRegistry->Ini->FLOOD_CHECK_TIME;
        /**
         * Do not apply this filter
         * to moderators (and admins)
         * Moderators can post as much as they want
         * as fast as they want
         *
         */
        if (!$this->oRegistry->Viewer->isModerator()) {
            switch ($this->eventName) {
                case 'onBeforeNewQuestion':
                    $this->byUser('QUESTIONS')->byIp('QUESTIONS');
                    break;

                case 'onBeforeNewAnswer':
                    $this->byUser('ANSWERS')->byIp('ANSWERS');
                    break;

                case 'onBeforeNewComment':
                    $this->checkComment();
                    break;
            }
        }
    }

    /**
     * Perfomes flood check for comments
     *
     * @throws \StudyBuddy\Exception is comment is
     * posted too soon after posting another comment
     */
    protected function checkComment() {
        d('cp');
        if (!$this->oRegistry->Viewer->isModerator()) {
            d('cp');
            $uid = $this->oRegistry->Viewer->getUid();
            $timeout = (int) $this->oRegistry->Ini->COMMENTS_FLOOD_TIME;
            d('timeout: ' . $timeout);
            $since = time() - $timeout;
            $where = array('i_uid' => $uid, 'i_ts' => array('$gt' => $since));

            $a = $this->oRegistry->Mongo->COMMENTS->findOne($where);

            if (!empty($a)) {
                throw new \StudyBuddy\Exception('You are posting too fast.<br>You must wait ' . $timeout . ' seconds between comments');
            }
        }
    }

    /**
     * @todo
     * Translate exception
     *
     * @throws QuestionParserException
     */
    protected function byIp($collName) {
        d('$collName: ' . $collName);
        if (!$this->oRegistry->Viewer->isModerator()) {
            $since = time() - ($this->minutesToWait * 60);
            $byIP = $this->oRegistry->Mongo
                    ->getCollection($collName)
                    ->findOne(array('ip' => $this->obj['ip'], 'i_ts' => array('$gt' => $since)), array('i_ts', 'hts'));


            if (!empty($byIP)) {
                throw new \StudyBuddy\FilterException('You are posting too fast. Please wait ' . $this->minutesToWait . ' minutes between posting');
            }
        }

        return $this;
    }

    protected function byUser($collName) {
        if (!$this->oRegistry->Viewer->isModerator()) {
            $since = time() - ($this->minutesToWait * 60);
            $byUid = $this->oRegistry->Mongo
                    ->getCollection($collName)
                    ->findOne(array('i_uid' => $this->obj['i_uid'], 'i_ts' => array('$gt' => $since)), array('i_ts', 'hts'));

            if (!empty($byUid)) {
                throw new \StudyBuddy\FilterException('You are posting too fast. Please wait ' . $this->minutesToWait . ' minutes between posting');
            }
        }

        return $this;
    }

}
